<?

class DerivadaClass
{
	public $function_string;
	public $derivative_variable = "x";
	public $L_matrix;
	public $N_matrix;

	// Construtor
	public function __construct($function_string)
	{
		$this->function_string = $function_string;
	}


	// Função para definir em qual variável será realizado a derivada
	public function DefineDerivativeVariable($var)
	{
		$this->derivative_variable = $var;
	}

	// Função para dar início a realização da derivada
	public function Derivar()
	{
		return $this->derivada($this->L_matrix[count($this->L_matrix)-1], $this->L_matrix);
	}

	//Função para dar início a substituição do L utilizando o substituir_L_latex
	public function ReplaceInLatex()
	{
		return $this->substituir_L_latex($this->N_matrix[count($this->N_matrix)-1], $this->N_matrix);
	}

	// Simplifica a equação com a substituição
	public function SimplifySub()
	{
		$s = $this->function_string;
		$s = str_replace(" ", "", $s);
		if(substr_count($s, "(") == substr_count($s, ")")){
			//echo $s."<br><br>";
			$m = 0;
			while(substr_count($s, "(")){
				$e = explode("(", $s);
				$n = count($e);
				$k = explode(")", $e[$n-1]);
				if(substr_count($k[0], "*") + substr_count($k[0], "/") > 1){
					while(substr_count($k[0], "*") + substr_count($k[0], "/") > 1){
					$u = explode("*", $k[0]);
					if(!substr_count($u[0], "/") && !substr_count($u[1], "/")){
						$b[0] = str_replace("-", "+-", $u[0]);
						$b[1] = str_replace("-", "+-", $u[1]);
						$x = explode("+", $b[0]);
						$y = explode("+", $b[1]);
						//echo "veio aqui".$x[count($x)-1]."ah";
						$L[$m] = $x[count($x)-1]."*".$y[0];
					}else if(substr_count($u[0], "/")){
						$b = explode("/", $u[0]);
						$b[0] = str_replace("-", "+-", $b[0]);
						$b[1] = str_replace("-", "+-", $b[1]);
						$x = explode("+", $b[0]);
						$y = explode("+", $b[1]);
						$L[$m] = $x[count($x)-1]."/".$y[0];
					}else if(substr_count($u[1], "/")){
						$b = explode("/", $u[1]);
						$b[0] = str_replace("-", "+-", $b[0]);
						$b[1] = str_replace("-", "+-", $u[0]);
						$x = explode("+", $b[1]);
						$y = explode("+", $b[0]);
						$L[$m] = $x[count($x)-1]."*".$y[0];
					}
					//echo "@L".$m."@ = ".$L[$m]."<br>";
					$k[0] = str_replace($L[$m], "@L".$m."@", $k[0]);
					$s = str_replace($L[$m], "@L".$m."@", $s);
					$m++;
					}
				}else{
					$L[$m] = $k[0];
					//echo "@L".$m."@ = ".$L[$m]."<br>";
					$s = str_replace("(".$L[$m].")", "@L".$m."@", $s);
					//echo $s."<br><br>";
					$m++;
				}
			}
			if(substr_count($s, "*") + substr_count($s, "/") > 1){
				while(substr_count($s, "*") + substr_count($s, "/") > 1){
					$u = explode("*", $s);
					if(!substr_count($u[0], "/") && !substr_count($u[1], "/")){
						$b[0] = str_replace("-", "+-", $u[0]);
						$b[1] = str_replace("-", "+-", $u[1]);
						$x = explode("+", $b[0]);
						$y = explode("+", $b[1]);
						$L[$m] = $x[count($x)-1]."*".$y[0];
					}else if(substr_count($u[0], "/")){
						$b = explode("/", $u[0]);
						$b[0] = str_replace("-", "+-", $b[0]);
						$b[1] = str_replace("-", "+-", $b[1]);
						$x = explode("+", $b[0]);
						$y = explode("+", $b[1]);
						$L[$m] = $x[count($x)-1]."/".$y[0];
					}else if(substr_count($u[1], "/")){
						$b = explode("/", $u[1]);
						$b[0] = str_replace("-", "+-", $b[0]);
						$b[1] = str_replace("-", "+-", $u[0]);
						$x = explode("+", $b[1]);
						$y = explode("+", $b[0]);
						$L[$m] = $x[count($x)-1]."*".$y[0];
					}
					//echo "@L".$m."@ = ".$L[$m]."<br>";
					$k[0] = str_replace($L[$m], "@L".$m."@", $k[0]);
					$s = str_replace($L[$m], "@L".$m."@", $s);
					$m++;
				}
			}
			$L[$m] = $s;
			//echo "@L".$m."@ = ".$L[$m]."<br>";
			//echo $s."<br><br>";
			//print_r($L);
			$this->L_matrix = $L;
		}else echo "Erro na entrada: há parêntesis definidos incorretamente.<br>";
	}

	// Transforma matriz de () em latex
	public function ToLatex(){
		$L = $this->L_matrix;
		$n = count($L);
		for($i=0; $i<$n; $i++){
			$s = $L[$i];
			if(substr_count($s, "*")){
				$e = explode("*", $s);
				$e[0] = str_replace("-", "+-", $e[0]);
				$e[1] = str_replace("-", "+-", $e[1]);
				$x = explode("+", $e[0]);
				if(substr($x[count($x)-1], 0, 1) == "-") $x[count($x)-1] = substr($x[count($x)-1], 1);
				$y = explode("+", $e[1]);
				$s = str_replace($x[count($x)-1].'*'.$y[0], '('.$x[count($x)-1].')('.$y[0].')', $s);
				$N[$i] = $s;
			}else if(substr_count($s, "/")){
				$e = explode("/", $s);
				$e[0] = str_replace("-", "+-", $e[0]);
				$e[1] = str_replace("-", "+-", $e[1]);
				$x = explode("+", $e[0]);
				if(substr($x[count($x)-1], 0, 1) == "-") $x[count($x)-1] = substr($x[count($x)-1], 1);
				$y = explode("+", $e[1]);
				$s = str_replace($x[count($x)-1].'/'.$y[0], '\frac{'.$x[count($x)-1].'}{'.$y[0].'}', $s);
				$s = str_replace("+-", "-", $s);
				$N[$i] = $s;
			}else $N[$i] = $s;
		}
		//echo "N=="; print_r($N);
		
		$this->N_matrix = $N;
		
	}

	// código para substituir L
	private function substituir_L($s, $L){
			$e = explode("@", $s);
			$c = count($e);
			for($u=1; $u<$c; $u=$u+2){
				$s = str_replace("@L".substr($e[$u], 1)."@", "(".$L[substr($e[$u], 1)].")", $s);
			}
			
			if(substr_count($s, "@"))
				return $this->substituir_L($s, $L);
			else{
				//$s = str_replace("--", "+", $s);
				//$s = str_replace("+-", "-", $s);
				return $s;
			}
	}

	// Código para realização da derivada
	public function derivada($s, $L){
		echo '$s ->'. $s. '<br>';
		$ns = ""; $pot = ""; $mult = "";
		if(substr_count($s, "*") || substr_count($s, "/") || substr_count($s, "@") || substr_count($s, "^".$this->derivative_variable)){
			$s = str_replace("-", "+-", $s);
			$r = explode("+", $s);
			$n = count($r);
			
			echo '$r[0] ->'. $r[0]. '<br>';
			echo '$r[1] ->'. $r[1]. '<br>';
			for($i=0; $i<$n; $i++){
				echo '$i ->'. $i. '<br>';
				if(substr_count($r[$i], "*")){
					$e = explode("*", $r[$i]);
					/*if(substr($e[0], 0, 1) == "@"){
						$u = explode("@", $e[0]);
						$e[0] = $L[substr($u[1], 1)];
					}
					if(substr($e[1], 0, 1) == "@"){
						$u = explode("@", $e[1]);
						$e[1] = $L[substr($u[1], 1)];
					}*/
					//echo '$e[0] = '.$e[0].'<br>';
					//echo '$e[1] = '.$e[1].'<br>';
					echo 'Use a derivada do produto, <img src="http://latex.codecogs.com/gif.latex?\frac{\mathrm{d} }{\mathrm{d} x}(uv)=\frac{du}{dx}v+\frac{dv}{dx}u">, onde <img src="http://latex.codecogs.com/gif.latex?u='.$this->substituir_L($e[0], $L).'"> e <img src="http://latex.codecogs.com/gif.latex?v='.$this->substituir_L($e[1], $L).'">:<br>';
					echo '= <img src="http://latex.codecogs.com/gif.latex?(\frac{\mathrm{d} }{\mathrm{d} x}('.$this->substituir_L($e[0], $L).'))('.$this->substituir_L($e[1], $L).')+(\frac{\mathrm{d} }{\mathrm{d} x}('.$this->substituir_L($e[1], $L).'))('.$this->substituir_L($e[0], $L).')"><br><br>';
					$ns .= "(".$this->derivada($e[0], $L).")*(".$this->substituir_L($e[1], $L).")+(".$this->derivada($e[1], $L).")*(".$this->substituir_L($e[0], $L).")";
					//echo '$ns = '.$ns.'<br>';
				}else if(substr_count($r[$i], "/")){
					$e = explode("/", $r[$i]);
					/*if(substr($e[0], 0, 1) == "@"){
						$u = explode("@", $e[0]);
						$e[0] = $L[substr($u[1], 1)];
					}
					if(substr($e[1], 0, 1) == "@"){
						$u = explode("@", $e[0]);
						$e[1] = $L[substr($u[1], 1)];
					}*/
					$ns .= "((".$this->derivada($e[0], $L).")*(".$this->substituir_L($e[1], $L).")-(".$this->derivada($e[1], $L).")*(".$this->substituir_L($e[0], $L)."))/(".$this->substituir_L($e[1], $L).")^2";
				}else if(substr_count($r[$i], "^")){
					$e = explode("^", $r[$i]);
					if(substr($e[0], 0, 1) == "@"){
						$u = explode("@", $e[0]);
						$e[0] = $L[substr($u[1], 1)];
					}else if(substr($e[0], -1) == $this->derivative_variable && strlen($e[0]) > 1){
						$mult = "(".substr($e[0], 0, -1).")*";
						$e[0] = substr($e[0], -1);
					}
					if(substr($e[1], 0, 1) == "@"){
						$u = explode("@", $e[1]);
						$e[1] = $L[substr($u[1], 1)];
					}
					if(substr_count($this->substituir_L($e[0], $L), $this->derivative_variable) && substr_count($this->substituir_L($e[1], $L), $this->derivative_variable)){
						$pot .= "(".$this->substituir_L($e[1], $L).")*((".$this->substituir_L($e[0], $L).")^(".$this->substituir_L($e[1], $L)."-1))*(".$this->derivada($e[0], $L).")+(".$this->derivada($e[1], $L).")*((".$this->substituir_L($e[0], $L).")^(".$this->substituir_L($e[1], $L)."))*ln(".$this->substituir_L($e[0], $L).")*(".$this->derivada($e[1], $L).")";
					}else if(substr_count($this->substituir_L($e[0], $L), $this->derivative_variable)){
						$pot .= "(".$this->derivada($e[0], $L).")*(".$this->substituir_L($e[1], $L).")*((".$this->substituir_L($e[0], $L).")^(".$this->substituir_L($e[1], $L)."-1))";
					}else{
						$pot .= "(".$this->derivada($e[1], $L).")*((".$this->substituir_L($e[0], $L).")^(".$this->substituir_L($e[1], $L)."))*ln(".$this->substituir_L($e[0], $L).")";
					}
					if($mult) $pot = $mult."(".$pot.")";
					$ns .= $pot;
				}else if(substr($r[$i], 0, 4) == "sqrt"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")/(2*sqrt(".$this->substituir_L($e[1], $L)."))";
				}else if(substr($r[$i], 0, 3) == "log"){
					$e = explode("@", $r[$i]);
					if(substr($e[0], 3) != "") 
						$baselog = substr($e[0], 3);
					else
						$baselog = "10";
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")/((".$this->substituir_L($e[1], $L).")*ln(".$baselog."))";
				}else if(substr($r[$i], 0, 2) == "ln"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")/(".$this->substituir_L($e[1], $L).")";
				}else if(substr($r[$i], 0, 4) == "senh"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*cosh(".$this->substituir_L($e[1], $L).")";
				}else if(substr($r[$i], 0, 3) == "sen"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*cos(".$this->substituir_L($e[1], $L).")";
				}else if(substr($r[$i], 0, 7) == "cossech"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*(-cossech(".$this->substituir_L($e[1], $L)."))*cotgh(".$this->substituir_L($e[1], $L).")";
				}else if(substr($r[$i], 0, 6) == "cossec"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*(-cossec(".$this->substituir_L($e[1], $L)."))*cotg(".$this->substituir_L($e[1], $L).")";
				}else if(substr($r[$i], 0, 4) == "cosh"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*senh(".$this->substituir_L($e[1], $L).")";
				}else if(substr($r[$i], 0, 3) == "cos"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*(-sen(".$this->substituir_L($e[1], $L)."))";
				}else if(substr($r[$i], 0, 3) == "tgh"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*sech^2(".$this->substituir_L($e[1], $L).")";
				}else if(substr($r[$i], 0, 2) == "tg"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*sec^2(".$this->substituir_L($e[1], $L).")";
				}else if(substr($r[$i], 0, 4) == "sech"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*(-tgh(".$this->substituir_L($e[1], $L)."))*sech(".$this->substituir_L($e[1], $L).")";
				}else if(substr($r[$i], 0, 3) == "sec"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*tg(".$this->substituir_L($e[1], $L).")*sec(".$this->substituir_L($e[1], $L).")";
				}else if(substr($r[$i], 0, 5) == "cotgh"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*(-cossech^2(".$this->substituir_L($e[1], $L)."))";
				}else if(substr($r[$i], 0, 4) == "cotg"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")*(-cossec^2(".$this->substituir_L($e[1], $L)."))";
				}else if(substr($r[$i], 0, 6) == "arcsenh"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")/sqrt((".$this->substituir_L($e[1], $L).")^2+1)";
				}else if(substr($r[$i], 0, 6) == "arcsen"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")/sqrt(1-(".$this->substituir_L($e[1], $L).")^2)";
				}else if(substr($r[$i], 0, 10) == "arccossech"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "-(".$this->derivada($e[1], $L).")/(sqrt(1+(1/(".$this->substituir_L($e[1], $L).")^2))*(".$this->substituir_L($e[1], $L).")^2)";
				}else if(substr($r[$i], 0, 9) == "arccossec"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "-(".$this->derivada($e[1], $L).")/((|".$this->substituir_L($e[1], $L)."|)*sqrt((".$this->substituir_L($e[1], $L).")^2-1))";
				}else if(substr($r[$i], 0, 7) == "arccosh"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "-(".$this->derivada($e[1], $L).")/sqrt((".$this->substituir_L($e[1], $L).")^2-1)";
				}else if(substr($r[$i], 0, 6) == "arccos"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "-(".$this->derivada($e[1], $L).")/sqrt(1-(".$this->substituir_L($e[1], $L).")^2)";
				}else if(substr($r[$i], 0, 6) == "arctgh"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")/(1-(".$this->substituir_L($e[1], $L).")^2)";
				}else if(substr($r[$i], 0, 5) == "arctg"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")/(1+(".$this->substituir_L($e[1], $L).")^2)";
				}else if(substr($r[$i], 0, 7) == "arcsech"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "-(".$this->derivada($e[1], $L).")/((".$this->substituir_L($e[1], $L).")*sqrt((1-".$this->substituir_L($e[1], $L).")/(1+".$this->substituir_L($e[1], $L)."))*(1+".$this->substituir_L($e[1], $L)."))";
				}else if(substr($r[$i], 0, 6) == "arcsec"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "(".$this->derivada($e[1], $L).")/((|".$this->substituir_L($e[1], $L)."|)*sqrt((".$this->substituir_L($e[1], $L).")^2-1))";
				}else if(substr($r[$i], 0, 7) == "arccotgh"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "-(".$this->derivada($e[1], $L).")/(1-(".$this->substituir_L($e[1], $L).")^2)";
				}else if(substr($r[$i], 0, 7) == "arccotg"){
					$e = explode("@", $r[$i]);
					$e[1] = $L[substr($e[1], 1)];
					$ns .= "-(".$this->derivada($e[1], $L).")/(1+(".$this->substituir_L($e[1], $L).")^2)";
				}
				else if(substr_count($r[$i], "@")){
					$u = explode("@", $r[$i]);
					$ns .= $this->derivada($L[substr($u[1], 1)], $L);
				}else $ns .= $this->derivative($r[$i]);
				
				if($i < $n-1) $ns .= "+";
			}
		}else{
			$ns .= $this->derivative($s);
			//echo '$ns = '.$ns.'<br>';
		}
		
		$ns = str_replace("+-", "-", $ns);
		return $ns;
	}

	private function getcharpot($s){
		$num = array();
		$n = strlen($s);
		for($i=0; $i<$n; $i++){
			if(is_numeric($s[$i]))
				$num[0] .= $s[$i];
			else
				$num[1] .= $s[$i];
		}
		
		return $num;
	}

	private function der($s){
		if(substr_count($s, $this->derivative_variable)){
			if(substr_count($s, "^")){
				$e = explode($this->derivative_variable."^", $s);
				if($e[1] == "0") return "";
				if($e[0] == ""){$e[0] = "1";}
				if($e[0] == "-") $e[0] = "-1";
				$bas = $this->getcharpot($e[0]);
				$exp = $this->getcharpot($e[1]);
				if($exp[0] == "") $exp[0] = 1;
				$multfrac = $bas[0]*$exp[0].$bas[1].$exp[1];
				if(is_numeric($e[1])) $opfrac = $e[1]-1; else $opfrac = "(".$e[1]."-1)";
				if($opfrac == "1") return $multfrac.$this->derivative_variable;
				if($opfrac == "0") return $multfrac;
				else return $multfrac.$this->derivative_variable."^".$opfrac;
			}else{
				$e = explode($this->derivative_variable, $s);
				if($e[0] == "" || $e[0] == "-") return str_replace($this->derivative_variable, "1", $s);
				else return str_replace($this->derivative_variable, "", $s);
			}
		}else return "";
	}

	private function derivative($s){
		$derivative = "";
		$s = str_replace("-", "+-", $s);
		if(substr_count($s, "+")){
			$e = explode("+", $s);
			$count = count($e);
			$is_der = 0;
			for($i=0; $i<$count; $i++)
			{
				$d = der($e[$i]);
				if($is_der && $d) $derivative .= "+";
				if($d){
					$derivative .= $d;
					$is_der = 1;
				}
			}
		}else{
			$d = $this->der($s);
			if($d){
				$derivative .= $d;
			}
		}
		
		return $derivative != "" ? $derivative : "0";
	}

	// código para substituir L em código já Latex
	private function substituir_L_latex($s, $N){
			$e = explode("@", $s);
			$c = count($e);
			for($u=1; $u<$c; $u=$u+2){
				if(substr($e[$u], 0, 1) == "L" && substr($e[$u+1], 0, 1) == "^" && substr($e[$u+2], 0, 1) == "L") $s = str_replace("@L".substr($e[$u], 1)."@^@L".substr($e[$u+2], 1)."@", "(".$N[substr($e[$u], 1)].")^{".$N[substr($e[$u+2], 1)]."}", $s);
				if(substr($e[$u], 0, 1) == "L" && substr($e[$u+1], 0, 1) == "^") $s = str_replace("@L".substr($e[$u], 1)."@^", "(".$N[substr($e[$u], 1)].")^", $s);
				else if(substr($e[$u-1], -1) == "^") $s = str_replace("^@L".substr($e[$u], 1)."@", "^{".$N[substr($e[$u], 1)]."}", $s);
				else if(substr($e[$u-1], -4) == "sqrt") $s = str_replace("sqrt@L".substr($e[$u], 1)."@", '\sqrt{'.$N[substr($e[$u], 1)].'}', $s);
				else if(substr($e[$u-1], -3) == "sen") $s = str_replace("sen@L".substr($e[$u], 1)."@", "sen(".$N[substr($e[$u], 1)].")", $s);
				else if(substr($e[$u-1], -3) == "cos") $s = str_replace("cos@L".substr($e[$u], 1)."@", "cos(".$N[substr($e[$u], 1)].")", $s);
				else if(substr($e[$u-1], -2) == "tg") $s = str_replace("tg@L".substr($e[$u], 1)."@", "tg(".$N[substr($e[$u], 1)].")", $s);
				else if(substr($e[$u-1], -4) == "cosh") $s = str_replace("cosh@L".substr($e[$u], 1)."@", "cosh(".$N[substr($e[$u], 1)].")", $s);
				else if(substr($e[$u-1], -4) == "senh") $s = str_replace("senh@L".substr($e[$u], 1)."@", "senh(".$N[substr($e[$u], 1)].")", $s);
				else if(substr($e[$u-1], -3) == "tgh") $s = str_replace("tgh@L".substr($e[$u], 1)."@", "tgh(".$N[substr($e[$u], 1)].")", $s);
				else if(substr($e[$u-1], -3) == "sec") $s = str_replace("sec@L".substr($e[$u], 1)."@", "sec(".$N[substr($e[$u], 1)].")", $s);
				else if(substr($e[$u-1], -4) == "sech") $s = str_replace("sech@L".substr($e[$u], 1)."@", "sec(".$N[substr($e[$u], 1)].")", $s);
				else if(substr($e[$u-1], -3) == "log") $s = str_replace("log@L".substr($e[$u], 1)."@", "log(".$N[substr($e[$u], 1)].")", $s);
				else if(substr($e[$u-1], -2) == "ln") $s = str_replace("ln@L".substr($e[$u], 1)."@", "ln(".$N[substr($e[$u], 1)].")", $s);
				else if(substr_count($e[$u-1], "log")){
					$a = explode("log", $e[$u-1]);
					$s = str_replace("log".$a[1]."@L".substr($e[$u], 1)."@", "log_{".$a[1]."}(".$N[substr($e[$u], 1)].")", $s);
				}
				else $s = str_replace("@L".substr($e[$u], 1)."@", $N[substr($e[$u], 1)], $s);
			}
			
			if(substr_count($s, "@"))
				return $this->substituir_L_latex($s, $N);
			else{
				$s = str_replace("--", "+", $s);
				$s = str_replace("+-", "-", $s);
				return $s;
			}
			
	}

}



//echo derivative("8")."<br>";
//$e = explode("+", "1/2+1/2+1/2", 2);
//print_r($e);









//$L = simp_subs("((3x^2)*(3x^3)-(2x)*(12x))/3x^2");
//echo $this->derivada($L[4], $L);
//print_r($D);

//$N = tolatex($L);
//echo substituir_L_latex($N[count($N)-1], $N);

//echo "<br>";
//echo 'd/dx = '.$this->derivada($L[count($L)-1], $L);

//derivative($L[count($L)-1], $L, $N);

?>